function createCalender(y, m) {
    var gzYear = calendar.solar2lunar(y, m, 1).gzYear;
    gzYear += calendar.solar2lunar(y, m, 1).Animal + "年";
    var data = {
        year: y,
        month: m,
        nyear: gzYear,
        days: getDays(y, m),
    };
    appendPaiban(data);
    addSpace(data);
    console.log(data);
    return data;
}

function addSpace(data) {
    //上个月的 
    if (data.days[0].week < 7) {
        console.log(data.days[0].week);
        var c = data.days[0].week;
        var sy = data.year;
        var sm = data.month;
        if (sm == 1) {
            sy = data.year - 1;
            sm = 13;
        }
        for (i = getMonthDays(sm - 1); i > getMonthDays(sm - 1) - c; i--) {
            data.days.unshift({});
        }
    }

    //下个月的
    if (data.days[data.days.length - 1].week <= 7) {
        var w = data.days[data.days.length - 1].week;
        w = w == 7 ? 0 : w;
        for (i = 0; i < 6 - w; i++) {
            data.days.push({});
        }
    }
}


function getBanStyle(num) {
    //0 休　１　白(主) 3 白(副) 5夜　8下
    var ban = ['label label-danger', 'label label-primary', '', 'label label-info', '', 'label label-default', '', '', 'label label-danger'];
    return ban[num];
}

function getBan(num) {
    //0 休　１　白(主) 3 白(副) 5夜　8下
    var ban = ['休', '白', '', '白夜', '', '下', '', '', '休'];
    return ban[num];
}

function appendPaiban(data) {
    var newb = ['1', '3', '5', '8'];
    var paiban = window.localStorage['paiban'];
    paiban = paiban != undefined ? JSON.parse(paiban) : {};
    for (var i = 0; i < data.days.length; i++) {
        var total = data.days[i].total;
        var temp = paiban[[data.year, data.month < 10 ? "0" + data.month : data.month, i + 1 < 10 ? "0" + (i + 1) : i + 1].join('-')];
        if (temp != undefined) {
            newb = temp;
        }
        var panIndex = newb[total % newb.length];
        data.days[i]['ban'] = getBan(panIndex);
        data.days[i]['banStyle'] = getBanStyle(panIndex);
    }
}

function getMonthDays(month) {
    var mdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    return mdays[month - 1];
}

function getDays(y, m) {
    var mdays = getMonthDays(m);
    if (m == 2) {
        var isLeap = isLeapYear(y);
        if (isLeap) {
            mdays = 29;
        }
    }
    var days = [];
    var nowDay = new Date();
    for (var i = 0; i < mdays; i++) {
        var term = calendar.solar2lunar(y, m, i + 1).Term;
        var ndayAndTerm = term != null ? term : calendar.solar2lunar(y, m, i + 1).IDayCn;
        //ndayAndTerm = [m, i + 1].join('-') == "1-1" ? "元旦" : ndayAndTerm;
        //ndayAndTerm = festival([m, i + 1].join('-'), ndayAndTerm);
        var d = i + 1;
        var week = calendar.solar2lunar(y, m, d).nWeek;
        days[i] = {
            'day': d,
            'week': week,
            'nmonth': calendar.solar2lunar(y, m, d).IMonthCn,
            'nday': ndayAndTerm,
            'total': get1900YtoNowDay([y, m < 10 ? "0" + m : m, d < 10 ? "0" + d : d].join('-')),
            'style': week == 6 || week == 7 ? '#e41212' : '#080808',
        };
        if (nowDay.getDate() == d && nowDay.getMonth() + 1 == m) {
            days[i].border = 1;
        }
    }
    return days;
}
